new Vue({
    el: '#app',
    data() {
        return {
            form: {
                Traceability_Acquisition_Order_SerialNumber: '',
                Acquisition_Date: '',
                ID_Front:'',
                ID_Number:'',
                ID_Name:'',
                Enterprise_Info_SerialNumber: '',
                Children: {
                    business_traceability_acquisition_order_detail: [{
                        Traceability_Acquisition_Order_Detail_SerialNumber: '',
                        Traceability_Acquisition_Order_SerialNumber: '',
                        Traceability_Farmer_SerialNumber: '',
                        Category_SerialNumber: '',
                        Article_SerialNumber: '',
                        Article_Specifications_SerialNumber: '',
                        Article_Unit_SerialNumber:'',
                        Quantity: '',
                        Unit_Price: '',
                        Acquisition_Type: '0',
                        Plate_Number: '',
                        Warehouse_SerialNumber: '',
                        specifications: [],
                    }]
                }
            },
            rules: {

            },
            farmer: [],
            category: [],
            article: [],
            unit:[],
            type: [{
                label: '下线收活',
                value: '0'
            }, {
                label: '村民送来',
                value: '1'
            }],
            warehouse: [],
            business_traceability_farmer:false,
            loading: false,
            iconLoading:false,
        }
    },
    mounted() {
        this.getData();
    },
    watch:{
        'form.ID_Front'(Data) {
            const that = this;
            that.iconLoading = true;
            $.ajax({
                url: `${BASE_URL1}/parseIDCardAction`,
                type: 'POST',
                data: { Data },
                success: function (Result) {
                    if (Result.Status == 'success') {
                        that.form.ID_Number = Result.ID_Number;
                        that.form.ID_Name = Result.name;
                    }
                },
                complete: function () {
                    that.iconLoading = false;
                },
                dataType: "json"
            });
        },
        'form.ID_Number'(val) {
            const that = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_traceability_farmer',
                    Condition: "Row_Status = '0' and ID_Number='"+ID_Number+"'",
                },
                success: function (Result) {
                    if (Result.length==0) {
                        that.business_traceability_farmer=true
                    }
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
    },
    methods: {
        getData() {
            const that = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_category',
                    Condition: "Row_Status = '0'",
                },
                success: function (Result) {
                    that.category = Result
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_traceability_farmer',
                    Condition: "Row_Status = '0'",
                },
                success: function (Result) {
                    that.farmer = Result
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_article_info',
                    Condition: "Row_Status = '0'",
                },
                success: function (Result) {
                    that.article = Result
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_article_unit',
                    Condition: "Row_Status = '0'",
                },
                success: function (Result) {
                    that.unit = Result
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_warehouse_info',
                    Condition: "Row_Status = '0'",
                },
                success: function (Result) {
                    that.warehouse = Result
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        change(val, row) {
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_article_specifications',
                    Condition: "Row_Status = '0' and Article_SerialNumber='" + val + "'",
                },
                success: function (Result) {
                    row.specifications = Result
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        add_business_traceability_acquisition_order_detail() {
            this.form.Children.business_traceability_acquisition_order_detail.push({
                Traceability_Acquisition_Order_Detail_SerialNumber: '',
                Traceability_Acquisition_Order_SerialNumber: '',
                Traceability_Farmer_SerialNumber: '',
                Category_SerialNumber: '',
                Article_SerialNumber: '',
                Article_Specifications_SerialNumber: '',
                Article_Unit_SerialNumber:'',
                Quantity: '',
                Unit_Price: '',
                Acquisition_Type: '0',
                Plate_Number: '',
                Warehouse_SerialNumber: '',
                specifications: [],
            })
        },
        remove_business_traceability_acquisition_order_detail() {
            if (this.form.Children.business_traceability_acquisition_order_detail.length > 1) {
                this.form.Children.business_traceability_acquisition_order_detail.pop()
            } else {
                this.$message({
                    message: '不能在删除了',
                    type: 'warning'
                })
            }
        },
        submitForm(formName) {
            const that = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    that.loading = false;
                    let sql = "";
                    that.form.Children.business_traceability_acquisition_order_detail.forEach(item => {
                        item.specificationsJSON = JSON.stringify(item.specifications);
                        if (sql) {
                            sql = sql + " OR Warehouse_SerialNumber='" + item.Warehouse_SerialNumber + "' AND Article_Specifications_SerialNumber='" + item.Article_Specifications_SerialNumber + "'\r\n"
                        } else {
                            sql = " Warehouse_SerialNumber='" + item.Warehouse_SerialNumber + "' AND Article_Specifications_SerialNumber='" + item.Article_Specifications_SerialNumber + "'\r\n"
                        }
                    })
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${BASE_URL2}/generalBusiness/generalQueryByTableNameWithOriginal`,
                        type: 'POST',
                        data: {
                            Data: 'business_warehouse_inventory',
                            Condition: "Row_Status = '0' and (" + sql + ")",
                        },
                        success: function (Result) {
                            let add_business_warehouse_inventory = [];
                            let modify_business_warehouse_inventory = [];
                            let inventory = [];
                            //聚合
                            that.form.Children.business_traceability_acquisition_order_detail.forEach(item => {
                                let obj = inventory.filter(i => i.Warehouse_SerialNumber == item.Warehouse_SerialNumber && i.Article_Specifications_SerialNumber == item.Article_Specifications_SerialNumber)[0]
                                if (obj) {
                                    obj.Inventory_Total_Quantity = accAdd(obj.Inventory_Total_Quantity, item.Quantity);
                                    obj.Remain_Quantity = accAdd(obj.Remain_Quantity, item.Quantity);
                                } else {
                                    inventory.push({
                                        Warehouse_Inventory_SerialNumber: '',
                                        Warehouse_SerialNumber: item.Warehouse_SerialNumber,
                                        Article_Specifications_SerialNumber: item.Article_Specifications_SerialNumber,
                                        Inventory_Total_Quantity: item.Quantity,
                                        Remain_Quantity: item.Quantity,
                                        Out_Total_Quantity: 0,
                                        Prediction_Quantity: 0,
                                        Amount: ''
                                    })
                                }
                            })
                            //分类
                            inventory.forEach(item => {
                                let modify = Result.filter(i => i.Warehouse_SerialNumber == item.Warehouse_SerialNumber && i.Article_Specifications_SerialNumber == item.Article_Specifications_SerialNumber)[0]
                                if (modify) {
                                    modify.Inventory_Total_Quantity = accAdd(modify.Inventory_Total_Quantity, item.Inventory_Total_Quantity);
                                    modify.Remain_Quantity = accAdd(modify.Remain_Quantity, item.Remain_Quantity);
                                    modify_business_warehouse_inventory.push(modify);
                                } else {
                                    add_business_warehouse_inventory.push(item);
                                }
                            })
                            let business_traceability_farmer = [];
                            if (that.business_traceability_farmer) {
                                business_traceability_farmer.push({
                                    ID_Number:that.form.ID_Number,
                                    ID_Name:that.form.ID_Name,
                                    ID_Front:that.form.ID_Front
                                })
                            }
                            $.ajax({
                                headers: requestHeaders(),
                                url: `${BASE_URL2}/generalBusiness/generalSave`,
                                type: 'POST',
                                data: {
                                    Data: JSON.stringify({ Add: {business_traceability_acquisition_order:[that.form] ,business_warehouse_inventory:add_business_warehouse_inventory,business_traceability_farmer} , Modify:{business_warehouse_inventory:modify_business_warehouse_inventory}})
                                },
                                success: function (res) {
                                    if (res.Status === 'success') {
                                        that.$message.success("提交成功")
                                        window.history.go(-1)
                                    } else {
                                        that.$message.error("提交失败，请稍后再试")
                                    }
                                },
                                complete: function (XMLHttpRequest) {
                                    that.loading = false
                                    requestPretreatment(XMLHttpRequest);
                                },
                                dataType: "json"
                            })
                        },
                        complete: function (XMLHttpRequest) {
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json"
                    });

                } else {
                    that.$message({
                        message: sumberError,
                        type: 'warning'
                    })
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    }
})